package vn.sunnet.backviet01.game.coloa.human.enemy;

import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import vn.sunnet.backviet01.game.coloa.constant.Constants;

public class Melee extends Enemy {
	private static long durationMove = 80;
	private static long durationAttack = 150;
	private static long durationDie = 150;
	
	private long countDelay = 0;
	
	public Melee(int tiledAttack, float pX, float pY,
			TiledTextureRegion pTiledTextureRegion) {
		super(tiledAttack, pX, pY, pTiledTextureRegion);
		this.name = Constants.MELEE;
	}

	public void onManagedUpdate(float pSecondsElapsed) {
		if (isAllowChangeStatus) {
			stopAnimation();
			if (this.status == Constants.DIE) {
				updateTextureRegion(mTextureRegion_next, true);
				
				animate(durationDie);
				this.setPosition(this.getX() + 30, this.getY() + 10);
			} else if (isFreezed && countDelay == 0) {
				updateTextureRegion(mTextureRegion_next, true);
				
				countDelay = System.currentTimeMillis();
				this.handler.setEnabled(false);
				this.setPosition(this.getX() + 20, this.getY());
			} else if (isStun && startStun == 0) {
				startStun = System.currentTimeMillis();
				setStunAction();
			} else if (this.status == Constants.ATTACK) {
				updateTextureRegion(mTextureRegion_next, true);
				
				animate(durationAttack);
			}
			isAllowChangeStatus = false;
		}
		else if (isFreezed && this.hitPoints > 0) {
			if (countDelay + timeFreezed < System.currentTimeMillis()) {
				resetMove();
				isFreezed = false;
				countDelay = 0;
			}
		} else if (isStun && this.hitPoints > 0) {
			if (timeStun + startStun < System.currentTimeMillis()) {
				resetStunAction();
				isStun = false;
				startStun = 0;
			}
		}
		
		if (status == Constants.ATTACK
				&& this.getCurrentTileIndex() != tiledAttack)
			isAttack = false;

		else if (status == Constants.DIE && this.life) {
			setAlpha(getAlpha() - 0.02f);
		}

		xContain = getX() + 10;
		yContain = getY() + 15;
		
		super.onManagedUpdate(pSecondsElapsed);
	}

	public void resetMove() {
		updateTextureRegion(mTextureRegionMove, true);
		this.setPosition(this.getX() - 30, this.getY() - 10);
		animate(durationMove);
		handler.setEnabled(true);
		status = Constants.MOVE;
	}
	
	public void setStunAction() {
		
		switch (status) {
		case Constants.MOVE:
			int index = this.getCurrentTileIndex();
			if (getTextureRegion() != mTextureRegionMove)
				updateTextureRegion(mTextureRegionMove, true);
			animate((long)(durationMove * 2));
			handler.setVelocityX(handler.getVelocityX() / 2);
			setCurrentTileIndex(index);
			
			break;
		case Constants.ATTACK:
			int index1 = this.getCurrentTileIndex();
			animate((long)(durationAttack * 2));
			setCurrentTileIndex(index1);
			break;
		default:
			break;
		}
	}
	
	public void resetStunAction() {
		switch (status) {
		case Constants.MOVE:
			int index = this.getCurrentTileIndex();
			if (getTextureRegion() != mTextureRegionMove)
				updateTextureRegion(mTextureRegionMove, true);
			animate(durationMove);
			handler.setVelocityX(handler.getVelocityX() * 2);
			setCurrentTileIndex(index);
			
			break;
		case Constants.ATTACK:
			int index1 = this.getCurrentTileIndex();
			animate(durationAttack);
			setCurrentTileIndex(index1);
			break;
		default:
			break;
		}
	}
	
}
